package com.wl156.activities;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.wl156.MyApp;
import com.wl156.lite.R;
import com.wl156.activities.find.FindCarMessage;
import com.wl156.activities.find.FindGood;
import com.wl156.activities.find.FindSpecialLine;
import com.wl156.activities.find.PlaceMap;
import com.wl156.activities.publish.NewCarInfo;
import com.wl156.activities.publish.NewGoodInfo;
import com.wl156.activities.transfor.BankCardToLogisticsCard;
import com.wl156.activities.transfor.History;
import com.wl156.activities.transfor.LcBalance;
import com.wl156.activities.transfor.LcToLc;
import com.wl156.activities.transfor.LogisticsCardToBankCard;
import com.wl156.model.Certificate;
import com.wl156.model.WebServiceClient;
import com.wl156.model.Wl156DB;

public class HomeActivity extends Activity implements OnClickListener {

	private static final String TAG = HomeActivity.class.getSimpleName();

	private static final int DIALOG_TRANSFOR = 1;

	private WebServiceClient mWebServiceClient;
	private Certificate mCertificate;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.act_home);

		mWebServiceClient = new WebServiceClient(this);
		mCertificate = ((MyApp) getApplication()).getCertificateFromXml();

		initMessageData();

		findViewById(R.id.btn_find_truck).setOnClickListener(this);
		findViewById(R.id.btn_find_good_message).setOnClickListener(this);
		findViewById(R.id.btn_find_special_line).setOnClickListener(this);
		findViewById(R.id.btn_order).setOnClickListener(this);
		findViewById(R.id.btn_my_place).setOnClickListener(this);
		findViewById(R.id.btn_shangmao).setOnClickListener(this);
		findViewById(R.id.my_feed).setOnClickListener(this);
		findViewById(R.id.btn_bank).setOnClickListener(this);
		findViewById(R.id.btn_publish_good_message).setOnClickListener(this);
		findViewById(R.id.btn_publish_truck_message).setOnClickListener(this);

		MyReciever myreciever = new MyReciever();
		IntentFilter intentFilter = new IntentFilter();
		intentFilter.addAction("has_new_message");
		registerReceiver(myreciever, intentFilter);
	}

	private void initMessageData() {
		TextView dingyue1 = (TextView) findViewById(R.id.dingyue1);
		TextView dingyue2 = (TextView) findViewById(R.id.dingyue2);
		TextView gonggao1 = (TextView) findViewById(R.id.gonggao);

		Wl156DB db = new Wl156DB(this);
		Cursor c = db
				.getReadableDatabase()
				.rawQuery(
						"select * from internal_message where type = '订阅信息' order by id desc",
						null);
		if (c.moveToNext()) {
			dingyue1.setText(c.getString(2));
		}
		if (c.moveToNext()) {
			dingyue2.setText(c.getString(2));
		}

		c = db.getReadableDatabase()
				.rawQuery(
						"select * from internal_message where type = '系统信息' order by id desc",
						null);
		if (c.moveToNext()) {
			gonggao1.setText(c.getString(2));
		}

		db.close();
	}

	class MyReciever extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {
			if (intent.getAction().equals("has_new_message")) {
				initMessageData();
			}
		}
	}

	@Override
	public void onClick(View v) {
		AlertDialog.Builder builder = new Builder(this);
		builder.setMessage("还没登录，请先登录");
		builder.setPositiveButton("登录", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				Intent i = new Intent(HomeActivity.this, Login.class);
				startActivity(i);
			}
		});
		builder.setNegativeButton("取消", null);
		if (!((MyApp) getApplication()).isLogined()) {
			builder.show();
			return;
		}

		switch (v.getId()) {
		case R.id.my_feed: {
			Intent i = new Intent(this, MessageCenter.class);
			startActivity(i);
		}
			break;
		case R.id.btn_find_truck: {
			Intent i = new Intent(this, FindCarMessage.class);
			startActivity(i);
		}
			break;
		case R.id.btn_find_good_message: {
			Intent i = new Intent(this, FindGood.class);
			startActivity(i);
		}
			break;
		case R.id.btn_find_special_line: {
			Intent i = new Intent();
			i.setClass(HomeActivity.this, FindSpecialLine.class);
			HomeActivity.this.startActivity(i);
		}
			break;
		case R.id.btn_order: {
			// TODO: 登陆后返回类型

			Class target = null;
			if (mCertificate.type.equals("司机")) {
				target = com.wl156.activities.order.DriverMyOrder.class;
			} else {
				target = com.wl156.activities.order.MyOrder.class;
			}

			Intent i = new Intent(this, target);
			startActivity(i);

			// XXX: XXX
			// Intent i = new Intent();
			// i.setClass(HomeActivity.this, ShangmaoXinxi.class);
			// HomeActivity.this.startActivity(i);
		}
			break;
		case R.id.btn_my_place: {
			Intent i = new Intent(this, PlaceMap.class);
			// i.putExtra("longitude", "120");
			// i.putExtra("latitude", "30");
			// i.putExtra("name", "我的位置");
			startActivity(i);
		}
			break;
		case R.id.login: {
			Intent i = new Intent(this, Login.class);
			startActivity(i);
		}
			break;
		case R.id.register: {
			Intent i = new Intent(this, Register.class);
			startActivity(i);
			break;
		}
		case R.id.btn_publish_truck_message: {
			Intent i = new Intent();
			i.setClass(HomeActivity.this, NewCarInfo.class);
			HomeActivity.this.startActivity(i);
		}
			break;
		case R.id.btn_publish_good_message: {
			Intent i = new Intent();
			i.setClass(HomeActivity.this, NewGoodInfo.class);
			HomeActivity.this.startActivity(i);
		}
			break;
		case R.id.btn_shangmao: {
			Intent i = new Intent();
			i.setClass(HomeActivity.this, ShangmaoXinxi.class);
			HomeActivity.this.startActivity(i);
		}
			break;
		case R.id.btn_bank: {
			Intent i = new Intent();
			i.setClass(HomeActivity.this, PhoneBank.class);
			HomeActivity.this.startActivity(i);
		}
			break;
		}
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		switch (id) {
		case DIALOG_TRANSFOR: {
			builder.setTitle("转账");
			final CharSequence[] items = { "银行卡到物流通卡", "物流通卡到银行卡", "物流通卡到物流通卡",
					"物流通卡余额", "往期记录" };
			builder.setItems(items, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int item) {
					Intent i = new Intent();
					Class startActivity = null;
					switch (item) {
					case 0:
						startActivity = BankCardToLogisticsCard.class;
						break;
					case 1:
						startActivity = LogisticsCardToBankCard.class;
						break;
					case 2:
						startActivity = LcToLc.class;
						break;
					case 3:
						startActivity = LcBalance.class;
						break;
					case 4:
						startActivity = History.class;
						break;
					}
					i.setClass(HomeActivity.this, startActivity);
					HomeActivity.this.startActivity(i);
				}
			});
		}
			break;
		}
		return builder.create();
	}

	@Override
	public void onBackPressed() {
		AlertDialog.Builder builder = new Builder(HomeActivity.this);
		builder.setMessage("确定退出吗？");
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				HomeActivity.super.onBackPressed();
			}
		});
		builder.setNegativeButton("取消", null);
		builder.show();
	}
}
